package com.example.houselease.controller;

import com.example.houselease.pojo.dto.admin.RoleAddNewDTO;
import com.example.houselease.pojo.dto.admin.PermissionDTO;
import com.example.houselease.pojo.dto.admin.RoleUpdateDTO;
import com.example.houselease.pojo.vo.RoleDetailVO;
import com.example.houselease.pojo.vo.RoleListVO;
import com.example.houselease.pojo.vo.RolePerDetailVO;
import com.example.houselease.service.IRoleService;
import com.example.houselease.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色 controller
 * @author sf
 * @version 0.0.1
 * */
@Slf4j
@RestController
@Api(tags = "2. 角色管理模块")
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 添加角色空请求的权限校验
     * */
    @GetMapping("/addPer")
    @ApiOperation("添加角色请求权限校验")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('house/role/add-new')")
    JsonResult<Void> addPer(){
        return JsonResult.ok();
    }

    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 101)
    @PreAuthorize("hasAnyAuthority('house/role/add-new')")
    JsonResult<Void> add(@Valid RoleAddNewDTO roleAddNewDTO){
        log.debug("开始处理【添加角色】请求，参数：{}",roleAddNewDTO);
        roleService.add(roleAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/reg-per/{id:[0-9]+}")
    @ApiOperation("注册角色权限")
    @ApiOperationSupport(order = 102)
    @ApiImplicitParam(name = "数据id", value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/role/add-new')")
    JsonResult<Void> regPer(@PathVariable Long id,
                            @RequestBody List<Long> permissionIds){
        log.debug("开始处理【注册角色权限】请求，参数1：{}，参数2：{}",id,permissionIds);
        permissionIds = permissionIds.stream().filter(i -> i != null).collect(Collectors.toList());
        roleService.regPer(id,permissionIds);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("角色列表")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('house/role/read')")
    JsonResult<List<RoleListVO>> list(){
        log.debug("开始处理【角色列表】请求");
        return JsonResult.ok(roleService.list());
    }

    @GetMapping("/name")
    @ApiOperation("角色名称是否重复")
    @ApiOperationSupport(order = 201)
    @PreAuthorize("hasAnyAuthority('house/role/read')")
    JsonResult<Void> nameIsAlready(String name){
        log.debug("开始处理【角色名称是否重复】请求，参数：{}",name);
        roleService.nameIsAlready(name);
        return JsonResult.ok();
    }

    @GetMapping("/per/{id:[0-9]+}")
    @ApiOperation("角色所拥有的权限id信息")
    @ApiOperationSupport(order = 202)
    @PreAuthorize("hasAnyAuthority('house/permission/read','house/role/read')")
    JsonResult<List<Long>> rolePer(@PathVariable Long id){
        log.debug("开始处理【角色所拥有的权限id信息】请求，参数：{}",id);
        return JsonResult.ok(roleService.getRolePerById(id));
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("角色详情")
    @ApiOperationSupport(order = 203)
    @PreAuthorize("hasAnyAuthority('house/permission/read')")
    JsonResult<RoleDetailVO> standard(@PathVariable Long id){
        log.debug("开始处理【角色详情】请求，参数：{}",id);
        return JsonResult.ok(roleService.getRoleDetail(id));
    }

    @DeleteMapping("/{id:[0-9]+}")
    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('house/permission/read','house/role/read')")
    JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【删除角色】请求，参数：{}",id);
        roleService.deleteById(id);
        return JsonResult.ok();
    }

    @PutMapping("/{id:[0-9]+}")
    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('house/role/update')")
    JsonResult<Void> update(@PathVariable Long id, @Valid RoleUpdateDTO roleUpdateDTO){
        log.debug("开始处理【修改角色】请求，参数1：{}，参数2：{}",id,roleUpdateDTO);
        roleService.update(id,roleUpdateDTO);
        return JsonResult.ok();
    }

    @PutMapping("/enable/{id:[0-9]+}")
    @ApiOperation("启用角色")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAnyAuthority('house/role/update')")
    JsonResult<Void> enable(@PathVariable Long id){
        log.debug("开始处理【启用角色】请求，参数：{}",id);
        roleService.enable(id);
        return JsonResult.ok();
    }

    @PutMapping("/disable/{id:[0-9]+}")
    @ApiOperation("禁用角色")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAnyAuthority('house/role/update')")
    JsonResult<Void> disable(@PathVariable Long id){
        log.debug("开始处理【禁用角色】请求，参数：{}",id);
        roleService.disable(id);
        return JsonResult.ok();
    }
}
